package com.zhehekeji.opsys.api.service.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.zhehekeji.opsys.api.entity.*;
import com.zhehekeji.opsys.api.resp.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.zhehekeji.opsys.api.cache.OperationCache;
import com.zhehekeji.opsys.api.common.util.DateFormatUtil;
import com.zhehekeji.opsys.api.common.util.ResultCode;
import com.zhehekeji.opsys.api.conf.Config;
import com.zhehekeji.opsys.api.enm.DefinitionFlag;
import com.zhehekeji.opsys.api.enm.ElementType;
import com.zhehekeji.opsys.api.service.CommonService;
import com.zhehekeji.opsys.api.util.Md5Utils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author chenweiliu chenwei.liu@163.com
 * @ClassName CmsOperationManagerService
 * @Description TODO
 * @date 2016-1-18   下午3:47:02
 */
@Slf4j
public class CmsOperationManagerService extends CommonService implements IOperationManagerService {


    @Autowired
    private OperationCache operationCache;

    @Autowired
    private Config config;

    @Override
    public GetProductListResp getProductList(Long cpCode, int firstIndex,
                                             int maxResult) {

        GetProductListResp resp = new GetProductListResp();
        if (cpCode == null) {
            log.error("cpCode非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("cpCode非法！");
            return resp;
        }
        String tmpWhereJpql = null;
        if (cpCode != -1) {
            tmpWhereJpql = "o.cpCode=" + cpCode + " and o.status=1";
        } else {
            tmpWhereJpql = "o.status=1";
        }
        try {
            String tmpSql = "select count(o.id) as count from com.zhehekeji.opsys.api.entity.TCmsProduct o where " + tmpWhereJpql;
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, null, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }

            LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
            orderBy.put("sequence", "desc");
            orderBy.put("createTime", "desc");
            List<TCmsProduct> tmpListTProduct = entityDao.getScrollData(TCmsProduct.class, firstIndex, maxResult, tmpWhereJpql,
                    null, orderBy);
            if (tmpListTProduct == null || tmpListTProduct.isEmpty()) return resp;
            List<GetProductListResp.ProductInfo> tmpListProductInfo = new ArrayList<GetProductListResp.ProductInfo>();
            GetProductListResp.ProductInfo tmpProductInfo;
            for (TCmsProduct tmpTProduct : tmpListTProduct) {
                tmpProductInfo = new GetProductListResp.ProductInfo();
                tmpProductInfo.setCode(tmpTProduct.getCode());
                tmpProductInfo.setCpCode(cpCode);
                tmpProductInfo.setCreateTime(DateFormatUtil.format(tmpTProduct.getCreateTime(), "yyyyMMddHHmmss"));
                tmpProductInfo.setDescription(tmpTProduct.getDescription());
                tmpProductInfo.setName(tmpTProduct.getName());
                tmpProductInfo.setPicture(tmpTProduct.getPicture());
                tmpProductInfo.setSequence(tmpTProduct.getSequence());
                tmpProductInfo.setStatus(tmpTProduct.getStatus());

                tmpListProductInfo.add(tmpProductInfo);
            }
            resp.setProductInfos(tmpListProductInfo);
            resp.setRecordSize(tmpIntCount);
            log.info("获取产品列表成功");
        } catch (Exception e) {
            log.error("获取产品列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取产品列表失败，程序异常！");
        }
        return resp;

    }

    @Override
    public GetProductDetailResp getProductDetail(String productCode) {

        GetProductDetailResp resp = new GetProductDetailResp();
        if (StringUtils.isEmpty(productCode)) {
            log.error("产品编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("产品编码非法！");
            return resp;
        }
        try {
            TCmsProduct tmpTProduct = entityDao.get(TCmsProduct.class, "o.code=?1 and o.status=1", new Object[]{productCode});
            if (tmpTProduct == null) {
                log.error("产品（" + productCode + "）不存在或者已停用！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("产品（" + productCode + "）不存在或者已停用！");
                return resp;
            }
            resp.setCode(tmpTProduct.getCode());
            resp.setCpCode(tmpTProduct.getCpCode());
            resp.setCreateTime(DateFormatUtil.format(tmpTProduct.getCreateTime(), "yyyyMMddHHmmss"));
            resp.setDescription(tmpTProduct.getDescription());
            resp.setName(tmpTProduct.getName());
            resp.setPicture(tmpTProduct.getPicture());
            resp.setSequence(tmpTProduct.getSequence());
            resp.setStatus(tmpTProduct.getStatus());
            log.info("获取产品详情成功");
        } catch (Exception e) {
            log.error("获取产品详情失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取产品详情失败，程序异常！");
        }
        return resp;

    }

    @Override
    public GetCategoryDetailResp getCategoryDetail(String categoryCode) {

        GetCategoryDetailResp resp = new GetCategoryDetailResp();
        if (StringUtils.isEmpty(categoryCode)) {
            log.error("栏目编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("栏目编码非法！");
            return resp;
        }
        try {
            TCmsCategory tmpTCategory = entityDao.get(TCmsCategory.class, "o.code=?1 and o.status=1", new Object[]{categoryCode});
            if (tmpTCategory == null) {
                log.error("栏目（" + categoryCode + "）不存在或者已停用！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("栏目（" + categoryCode + "）不存在或者已停用！");
                return resp;
            }

            resp.setCode(tmpTCategory.getCode());
            resp.setCpCode(tmpTCategory.getCpCode());
            resp.setCreateTime(DateFormatUtil.format(tmpTCategory.getCreateTime(), "yyyyMMddHHmmss"));
            resp.setDescription(tmpTCategory.getDescription());
            resp.setName(tmpTCategory.getName());
            resp.setParentCode(tmpTCategory.getParentCode());
            resp.setImgAddress(config.getImgAddress());
            resp.setPoster(getHttpUrl(tmpTCategory.getPoster()));
            resp.setSequence(tmpTCategory.getSequence());
            resp.setStatus(tmpTCategory.getStatus());
            resp.setThumbnal(getHttpUrl(tmpTCategory.getThumbnal()));
            resp.setFocusPicture(getHttpUrl(tmpTCategory.getFocusPicture()));
            resp.setUnfocusPicture(getHttpUrl(tmpTCategory.getUnfocusPicture()));
            resp.setDisplayFlag(tmpTCategory.getDisplayFlag());
            resp.setTags(tmpTCategory.getTags());
            log.info("获取栏目详情成功");
        } catch (Exception e) {
            log.error("获取栏目详情失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取栏目详情失败，程序异常！");
        }
        return resp;

    }

    @Override
    public GetCategoryListResp getCategoryList(Long cpCode, String categoryCode,
                                               int firstIndex, int maxResult) {

        GetCategoryListResp resp = new GetCategoryListResp();
        if (StringUtils.isEmpty(categoryCode)) {
            log.error("栏目编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("栏目编码非法！");
            return resp;
        }
        try {
            String tmpSql = "select count(o.id) as count from TCmsCategory o where o.parentCode=? and o.status=1";
            String tmpJpqlWhere = "o.parentCode=?1 and o.status=1";
            if (cpCode != null && cpCode != -1) {
                tmpSql = tmpSql + " and o.cpCode=" + cpCode;
                tmpJpqlWhere = tmpJpqlWhere + " and o.cpCode=" + cpCode;
            }
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{categoryCode}, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }

            LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
            orderBy.put("sequence", "desc");
            orderBy.put("createTime", "desc");
            List<TCmsCategory> tmpListTCategory = entityDao.getScrollData(TCmsCategory.class, firstIndex, maxResult, tmpJpqlWhere,
                    new Object[]{categoryCode}, orderBy);
            if (tmpListTCategory == null || tmpListTCategory.isEmpty()) {
                return resp;
            }

            List<GetCategoryListResp.CategoryInfo> tmpListCategoryInfo = new ArrayList<GetCategoryListResp.CategoryInfo>();
            GetCategoryListResp.CategoryInfo tmpCategoryInfo;
            for (TCmsCategory tmpTCategory : tmpListTCategory) {
                tmpCategoryInfo = new GetCategoryListResp.CategoryInfo();
                tmpCategoryInfo.setCode(tmpTCategory.getCode());
                tmpCategoryInfo.setCpCode(tmpTCategory.getCpCode());
                tmpCategoryInfo.setCreateTime(DateFormatUtil.format(tmpTCategory.getCreateTime(), "yyyyMMddHHmmss"));
                tmpCategoryInfo.setDescription(tmpTCategory.getDescription());
                tmpCategoryInfo.setProvince(tmpTCategory.getProvince());
                tmpCategoryInfo.setName(tmpTCategory.getName());
                tmpCategoryInfo.setParentCode(tmpTCategory.getParentCode());
                tmpCategoryInfo.setImgAddress(config.getImgAddress());
                tmpCategoryInfo.setPoster(getHttpUrl(tmpTCategory.getPoster()));
                tmpCategoryInfo.setSequence(tmpTCategory.getSequence());
                tmpCategoryInfo.setStatus(tmpTCategory.getStatus());
                tmpCategoryInfo.setThumbnal(getHttpUrl(tmpTCategory.getThumbnal()));
                tmpCategoryInfo.setFocusPicture(getHttpUrl(tmpTCategory.getFocusPicture()));
                tmpCategoryInfo.setUnfocusPicture(getHttpUrl(tmpTCategory.getUnfocusPicture()));
                tmpCategoryInfo.setDisplayFlag(tmpTCategory.getDisplayFlag());
                tmpCategoryInfo.setTags(tmpTCategory.getTags());
                tmpListCategoryInfo.add(tmpCategoryInfo);
            }
            resp.setCategoryInfos(tmpListCategoryInfo);
            resp.setRecordSize(tmpIntCount);
            log.info("获取栏目列表成功");
        } catch (Exception e) {
            log.error("获取栏目列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取栏目列表失败，程序异常！");
        }
        return resp;

    }

    /**
     * (非 Javadoc)
     * <p>Title: getContentList</p>
     * <p>Description: 栏目下挂靠的节目类型限制只能是剧头、普通VOD，未支持直接挂靠子集，返回TCmsSeries的编码</p>
     *
     * @param categoryCode
     * @param firstIndex
     * @param maxResult
     * @return
     * @see IOperationManagerService#getContentList(java.lang.String, int, int)
     */
    @Override
    public GetContentListResp getContentList(String categoryCode, String tag, String order, int firstIndex, int maxResult) {

        GetContentListResp resp = new GetContentListResp();
        if (StringUtils.isEmpty(categoryCode)) {
            log.error("栏目编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("栏目编码非法！");
            return resp;
        }
        try {
        	String where = "";
        	String asc = "desc";
        	if(StringUtils.equals(order, "2")) {//1正序   2倒序
        		asc = "asc";
        	}
        	if(StringUtils.isNotBlank(tag)) {
        		where += " and p.tags like '%"+tag+"%'";
        	}
        	
            //String tmpSql = "select count(o.id) as count from TCmsMapping o,TCmsSeries p where o.categoryCode=? and o.status=1 and o.seriesCode=p.code and p.status=1";
            //不对节目集状态做限制
            String tmpSql = "select count(o.id) as count from TCmsMapping o,TCmsSeries p where o.categoryCode=? "+where+" and o.status=1 and p.status=1 and o.seriesCode=p.code";
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{categoryCode}, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }
            String tmpProductCode = getProductCode(categoryCode);
            TCmsProduct tmpTProduct = entityDao.get(TCmsProduct.class, "o.code=?1 and o.status=1", new Object[]{tmpProductCode});

            String tmpObjectSql = "select p as p from TCmsMapping o,TCmsSeries p where o.categoryCode=? "+where+" and o.status=1 and p.status=1 and o.seriesCode=p.code "
                    //+ "and p.status=1 order by o.sequence desc,o.createTime desc";
                    //不对节目集状态做限制
                    + "order by o.createTime "+asc+", o.sequence "+asc;//o.randomNumber desc,
            List<Map<String, Object>> tmpListMapObject = entityDao.getScrollDataByHql(tmpObjectSql, new Object[]{categoryCode}, firstIndex, maxResult);
            if (tmpListMapObject == null || tmpListMapObject.isEmpty()) {
                return resp;
            }

//			TCmsSeries tmpSeries;
//			List<TCmsSeries> tmpListTCmsSeries = new ArrayList<TCmsSeries>();
//			StringBuilder tmpSBInSql = new StringBuilder("");
//			for (Map<String, Object> tmpItem : tmpListMapObject){
//				tmpSeries = (TCmsSeries) tmpItem.get("p");
//				if (tmpSeries == null) continue;
//				tmpListTCmsSeries.add(tmpSeries);
//				//0表示普通VOD，普通VOD有时长，节目programId
//				if (tmpSeries.getSeriesFlag() == 0){
//					tmpSBInSql.append("'").append(tmpSeries.getCode()).append("',");
//				}
//			}
//			
//			Map<String,TCmsProgram> tmpTCmsProgramMap = new HashMap<String, TCmsProgram>();
//			if(tmpSBInSql.length() > 0) {
//				tmpSBInSql = tmpSBInSql.deleteCharAt(tmpSBInSql.length() - 1);
//				List<TCmsProgram> tmpListTCmsProgram = entityDao.getScrollData(TCmsProgram.class, "o.seriesCode in(" + tmpSBInSql.toString() + ")", null);
//				if (tmpListTCmsProgram != null){
//					for (TCmsProgram tmpItm : tmpListTCmsProgram){
//						tmpTCmsProgramMap.put(tmpItm.getSeriesCode(), tmpItm);
//					}
//				}
//			}

            List<GetContentListResp.ContentInfo> tmpListContentInfo = new ArrayList<GetContentListResp.ContentInfo>();
            GetContentListResp.ContentInfo tmpContentInfo;
            Integer tmpSeriesFlag;
            TCmsSeries tmpTCmsSeries;
            for (Map<String, Object> tmpItem : tmpListMapObject) {
                tmpTCmsSeries = (TCmsSeries) tmpItem.get("p");
                if (tmpTCmsSeries == null) {
                    continue;
                }
                tmpContentInfo = new GetContentListResp.ContentInfo();
                tmpSeriesFlag = tmpTCmsSeries.getSeriesFlag();
                if (tmpSeriesFlag == null) {
                    log.error("数据错误，内容元素类型为空！");
                    resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                    resp.setResultDesc("数据错误，内容元素类型为空！");
                    return resp;
                }
                tmpContentInfo.setActor(tmpTCmsSeries.getActorDisplay());
                tmpContentInfo.setClarity(DefinitionFlag.toDesc(tmpTCmsSeries.getSeriesFlag()));
                tmpContentInfo.setCopyrightEndTime(tmpTCmsSeries.getLicensingWindowEnd());
                tmpContentInfo.setCopyrightStartTime(tmpTCmsSeries.getLicensingWindowStart());
                tmpContentInfo.setCpCode(tmpTCmsSeries.getCpCode());
                tmpContentInfo.setCreateTime(DateFormatUtil.format(tmpTCmsSeries.getCreateTime(), "yyyyMMddHHmmss"));
                tmpContentInfo.setDescription(tmpTCmsSeries.getDescription());
                tmpContentInfo.setDirector(tmpTCmsSeries.getDirectorDisplay());

                tmpContentInfo.setCode(tmpTCmsSeries.getCode());
                tmpContentInfo.setName(tmpTCmsSeries.getName());
                tmpContentInfo.setVolumnCount(tmpTCmsSeries.getVolumnCount());
                tmpContentInfo.setUpdateCount(tmpTCmsSeries.getUpdateCount());
                //0表示普通VOD，普通VOD有时长，节目programId
                if (tmpSeriesFlag == 0) {
                    tmpContentInfo.setElementType(ElementType.PROGRAM.getType());
                    //1表示连续剧剧头，没有时长
                } else if (tmpSeriesFlag == 1) {
                    tmpContentInfo.setElementType(ElementType.SERIES.getType());
                    //剧头
                    //tmpContentInfo.setParentCode();
                    //tmpContentInfo.setParentName();
                }
                tmpContentInfo.setLanguage(tmpTCmsSeries.getLanguage());
                //tmpContentInfo.setMappingID();
                //tmpContentInfo.setMovieID(movieID);
                tmpContentInfo.setOriginalCountry(tmpTCmsSeries.getOriginalCountry());
                //tmpContentInfo.setPhysicalContentID();
                //tmpContentInfo.setPlayUrl();
                tmpContentInfo.setImgAddress(config.getImgAddress());
                //TODO URL规则待定
                tmpContentInfo.setPoster(getHttpUrl(tmpTCmsSeries.getPosterUrl()));
                tmpContentInfo.setPrice(tmpTCmsSeries.getPriceTaxIn());
                if (tmpTProduct != null) {
                    tmpContentInfo.setProductCode(tmpProductCode);
                    tmpContentInfo.setProductName(tmpTProduct.getName());
                }
                tmpContentInfo.setProgramType(tmpTCmsSeries.getType());
                //TODO URL规则待定
                tmpContentInfo.setRecommendImg(getHttpUrl(tmpTCmsSeries.getRecommendUrl()));
                //tmpContentInfo.setRecommendSeries();
                tmpContentInfo.setReleaseYear(tmpTCmsSeries.getReleaseYear());
                tmpContentInfo.setSearchName(tmpTCmsSeries.getSearchName());
                tmpContentInfo.setStatus(tmpTCmsSeries.getStatus());
                //TODO URL规则待定
                tmpContentInfo.setStillImg(getHttpUrl(tmpTCmsSeries.getStillUrl()));
                //具体影片文件才有存储地址
                //tmpContentInfo.setStoreAddress();
                //TODO URL规则待定
                tmpContentInfo.setThumbnail(getHttpUrl(tmpTCmsSeries.getThumbnailUrl()));
                tmpContentInfo.setWriters(tmpTCmsSeries.getWriterDisplay());
                tmpContentInfo.setTags(tmpTCmsSeries.getTags());
                tmpContentInfo.setCmdId(tmpTCmsSeries.getCmdId());
                tmpListContentInfo.add(tmpContentInfo);
            }
            resp.setContentInfos(tmpListContentInfo);
            resp.setRecordSize(tmpIntCount);
            log.info("获取内容列表成功");
        } catch (Exception e) {
            log.error("获取内容列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取内容列表失败，程序异常！");
        }
        return resp;

    }

    private String getProductCode(String categoryCode) throws SQLException {
        TCmsCategory tmpCategory = entityDao.get(TCmsCategory.class, "o.code=?1", new Object[]{categoryCode});
        if (tmpCategory == null) {
            return null;
        }
        return getProductCode(tmpCategory);
    }

    private String getProductCode(TCmsCategory category) throws SQLException {
        TCmsCategory tmpParentCategory = entityDao.get(TCmsCategory.class, "o.code=?1", new Object[]{category.getParentCode()});
        if (tmpParentCategory == null) {
            TCmsProductCategory tmpTCmsProductCategory = entityDao.get(TCmsProductCategory.class, "o.categoryCode=?1", new Object[]{category.getCode()});
            if (tmpTCmsProductCategory != null) {
                return tmpTCmsProductCategory.getProductCode();
            } else {
                return null;
            }
        }
        return getProductCode(tmpParentCategory);
    }

    /**
     * (非 Javadoc)
     * <p>Title: getContentDetail</p>
     * <p>Description: contentCode 可以是TCmsProgram的编码也可以是TCmsSeries的编码</p>
     *
     * @param contentCode
     * @param productCode
     * @return
     * @see IOperationManagerService#getContentDetail(java.lang.String, java.lang.String)
     */
    @Override
    public GetContentDetailResp getContentDetail(String contentCode,
                                                 String productCode) {

        GetContentDetailResp resp = new GetContentDetailResp();
        if (StringUtils.isEmpty(contentCode)) {
            log.error("内容编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("内容编码非法！");
            return resp;
        }
        try {
            /**
             * 判断内容编码是节目集还是节目编码
             */

            TCmsProgram tmpTProgram = entityDao.get(TCmsProgram.class, "o.code=?1 and o.status=1", new Object[]{contentCode});
            //不对节目状态做限制
            //TCmsProgram tmpTProgram = entityDao.get(TCmsProgram.class, "o.code=?1", new Object[]{contentCode});
            TCmsSeries tcmsSeries = entityDao.get(TCmsSeries.class, "o.code=?1 and o.status=1", new Object[]{contentCode});
            //不对节目集状态做限制
            //TCmsSeries tcmsSeries = entityDao.get(TCmsSeries.class, "o.code=?1", new Object[]{contentCode});
            if (tmpTProgram == null && tcmsSeries == null) {
                log.error("内容（" + contentCode + "）不存在！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("内容（" + contentCode + "）不存在！");
                return resp;
            }
            if (tmpTProgram != null && tcmsSeries != null) {
                log.error("内容（" + contentCode + "）数据错误，同时存在节目与节目集！");
                resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                resp.setResultDesc("内容（" + contentCode + "）数据错误，同时存在节目与节目集！");
                return resp;
            }
            TCmsProduct tmpTProduct = null;
            if (!StringUtils.isBlank(productCode)) {
                tmpTProduct = entityDao.get(TCmsProduct.class, "o.code=?1 and o.status=1", new Object[]{productCode});
            }

            resp.setImgAddress(config.getImgAddress());

            /**
             * TCmsProgram的编码
             */
            if (tmpTProgram != null) {
                Integer tmpSeriesFlag = tmpTProgram.getSeriesFlag();
                if (tmpSeriesFlag == null) {
                    log.error("数据错误，内容元素类型为空！");
                    resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                    resp.setResultDesc("数据错误，内容元素类型为空！");
                    return resp;
                }
                resp.setAliasName(tmpTProgram.getOriginalName());
                resp.setHuaWeiCode(tmpTProgram.getHuaWeiCode());
                
                TCmsSeries tmpTCmsSeries = entityDao.get(TCmsSeries.class, "o.code=?1 and o.status=1",
                //		new Object[]{tmpTProgram.getSeriesCode()});
                //不对节目集状态做限制
                //TCmsSeries tmpTCmsSeries = entityDao.get(TCmsSeries.class, "o.code=?1",
                        new Object[]{tmpTProgram.getSeriesCode()});
                if (tmpTCmsSeries == null) {
                    log.error("数据错误，节目集为空！");
                    resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                    resp.setResultDesc("数据错误，节目集为空！");
                    return resp;
                }

                resp.setProductCode(productCode);
                if (tmpTProduct != null) {
                    resp.setProductName(tmpTProduct.getName());
                }
                resp.setBackground(tmpTCmsSeries.getReserve5());
                resp.setCode(tmpTProgram.getCode());
                resp.setName(tmpTProgram.getName());
                resp.setDuration(tmpTProgram.getDuration());
                resp.setProgramID(tmpTProgram.getCode());
                resp.setSetNumber(tmpTProgram.getSerialNo());
                resp.setTags(tmpTCmsSeries.getTags());

                TCompany tCompany = entityDao.get(TCompany.class, "o.code=?1", new Object[]{tmpTCmsSeries.getCpCode()});
                if (null != tCompany) {
                    resp.setCpCode(tmpTCmsSeries.getCpCode());
                    resp.setCpName(tCompany.getName());
                }
                //普通VOD，获取节目集数据
                if (tmpSeriesFlag == 0) {
                    resp.setActor(tmpTCmsSeries.getActorDisplay());
                    resp.setClarity(DefinitionFlag.toDesc(tmpTCmsSeries.getDefinitionFlag()));
                    resp.setCopyrightEndTime(tmpTCmsSeries.getLicensingWindowEnd());
                    resp.setCopyrightStartTime(tmpTCmsSeries.getLicensingWindowStart());
                    resp.setCreateTime(DateFormatUtil.format(tmpTCmsSeries.getCreateTime(), "yyyyMMddHHmmss"));
                    resp.setDescription(tmpTCmsSeries.getDescription());
                    resp.setDirector(tmpTCmsSeries.getDirectorDisplay());
                    resp.setElementType(ElementType.PROGRAM.getType());
                    //resp.setHuaWeiPlayUrl();
                    resp.setLanguage(tmpTCmsSeries.getLanguage());
                    //resp.setMappingID();
                    //resp.setMovieID();
                    resp.setOriginalCountry(tmpTCmsSeries.getOriginalCountry());
                    //普通VOD没有剧头
                    //resp.setParentCode();
                    //resp.setParentName();
                    //resp.setPhysicalContentID();
                    //resp.setPlayUrl();
                    //TODO URL规则待定
                    resp.setPoster(getHttpUrl(tmpTCmsSeries.getPosterUrl()));
                    resp.setPrice(tmpTCmsSeries.getPriceTaxIn());
                    resp.setProgramType(tmpTCmsSeries.getType());
                    //TODO URL规则待定
                    resp.setRecommendImg(getHttpUrl(tmpTCmsSeries.getRecommendUrl()));
                    //resp.setRecommendSeries();
                    resp.setReleaseYear(tmpTCmsSeries.getReleaseYear());
                    resp.setSearchName(tmpTCmsSeries.getSearchName());
                    resp.setStatus(tmpTCmsSeries.getStatus());
                    //TODO URL规则待定
                    resp.setStillImg(getHttpUrl(tmpTCmsSeries.getStillUrl()));
                    //resp.setStoreAddress();
                    //TODO URL规则待定
                    resp.setThumbnail(getHttpUrl(tmpTCmsSeries.getThumbnailUrl()));
                    //resp.setVolumnCount();
                    resp.setWriters(tmpTCmsSeries.getWriterDisplay());

                    if (!StringUtils.isEmpty(tmpTProgram.getHuaWeiPlayUrl())) {
                        resp.setHuaWeiPlayUrl(genSecurityMd5Url(tmpTProgram.getHuaWeiPlayUrl()));
                    }

                    if (!StringUtils.isEmpty(tmpTProgram.getZhongXingPlayUrl())) {
                        resp.setZhongXingPlayUrl(tmpTProgram.getZhongXingPlayUrl());
                    }
                    resp.setCmdId(tmpTProgram.getCmdID());
                    //连续剧剧集，获取节目数据
                } else if (tmpSeriesFlag == 1) {
                    resp.setActor(tmpTProgram.getActorDisplay());
                    resp.setClarity(DefinitionFlag.toDesc(tmpTProgram.getDefinitionFlag()));
                    resp.setCopyrightEndTime(tmpTProgram.getLicensingWindowEnd());
                    resp.setCopyrightStartTime(tmpTProgram.getLicensingWindowStart());
                    resp.setCpCode(tmpTProgram.getCpCode());
                    resp.setCreateTime(DateFormatUtil.format(tmpTProgram.getCreateTime(), "yyyyMMddHHmmss"));
                    resp.setDescription(tmpTProgram.getDescription());
                    resp.setDirector(tmpTProgram.getDirectorDisplay());
                    resp.setElementType(ElementType.CSERIES.getType());
                    //resp.setHuaWeiPlayUrl();
                    resp.setLanguage(tmpTProgram.getLanguage());
                    //resp.setMappingID();
                    //resp.setMovieID();
                    resp.setOriginalCountry(tmpTProgram.getOriginalCountry());
                    resp.setParentCode(tmpTCmsSeries.getCode());
                    resp.setParentName(tmpTCmsSeries.getName());
                    //resp.setPhysicalContentID();
                    //resp.setPlayUrl();
                    //TODO URL规则待定
                    resp.setPoster(getHttpUrl(tmpTProgram.getPosterUrl()));
                    resp.setPrice(tmpTProgram.getPriceTaxIn());
                    resp.setProgramType(tmpTProgram.getType());
                    //TODO URL规则待定
                    resp.setRecommendImg(getHttpUrl(tmpTProgram.getRecommendUrl()));
                    //resp.setRecommendSeries();
                    resp.setReleaseYear(tmpTProgram.getReleaseYear());
                    resp.setSearchName(tmpTProgram.getSearchName());
                    resp.setStatus(tmpTProgram.getStatus());
                    //TODO URL规则待定
                    resp.setStillImg(getHttpUrl(tmpTProgram.getStillUrl()));
                    //resp.setStoreAddress();
                    //TODO URL规则待定
                    resp.setThumbnail(getHttpUrl(tmpTProgram.getThumbnailUrl()));
                    //resp.setVolumnCount();
                    resp.setWriters(tmpTProgram.getWriterDisplay());

                    if (!StringUtils.isEmpty(tmpTProgram.getHuaWeiPlayUrl())) {
                        resp.setHuaWeiPlayUrl(genSecurityMd5Url(tmpTProgram.getHuaWeiPlayUrl()));
                    }

                    if (!StringUtils.isEmpty(tmpTProgram.getZhongXingPlayUrl())) {
                        resp.setZhongXingPlayUrl(tmpTProgram.getZhongXingPlayUrl());
                    }
                    resp.setCmdId(tmpTProgram.getCmdID());
                }
                /**
                 * TCmsSeries的编码
                 */
            } else {
                Integer tmpSeriesFlag = tcmsSeries.getSeriesFlag();
                if (tmpSeriesFlag == null) {
                    log.error("数据错误，内容元素类型为空！");
                    resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                    resp.setResultDesc("数据错误，内容元素类型为空！");
                    return resp;
                }
                resp.setProductCode(productCode);
                if (tmpTProduct != null) {
                    resp.setProductName(tmpTProduct.getName());
                }
                resp.setTags(tcmsSeries.getTags());
                //普通VOD，获取节目集数据
                if (tmpSeriesFlag == 0) {
                    tmpTProgram = entityDao.get(TCmsProgram.class, "o.seriesCode=?1", new Object[]{contentCode});
                    resp.setCode(tmpTProgram.getCode());
                    resp.setName(tmpTProgram.getName());
                    resp.setDuration(tmpTProgram.getDuration());
                    resp.setProgramID(tmpTProgram.getCode());
                    resp.setSetNumber(tmpTProgram.getSerialNo());

                    resp.setActor(tcmsSeries.getActorDisplay());
                    resp.setClarity(DefinitionFlag.toDesc(tcmsSeries.getDefinitionFlag()));
                    resp.setCopyrightEndTime(tcmsSeries.getLicensingWindowEnd());
                    resp.setCopyrightStartTime(tcmsSeries.getLicensingWindowStart());
                    resp.setCpCode(tcmsSeries.getCpCode());
                    resp.setCreateTime(DateFormatUtil.format(tcmsSeries.getCreateTime(), "yyyyMMddHHmmss"));
                    resp.setDescription(tcmsSeries.getDescription());
                    resp.setDirector(tcmsSeries.getDirectorDisplay());
                    resp.setElementType(ElementType.PROGRAM.getType());
                    //resp.setHuaWeiPlayUrl();
                    resp.setLanguage(tcmsSeries.getLanguage());
                    //resp.setMappingID();
                    //resp.setMovieID();
                    resp.setOriginalCountry(tcmsSeries.getOriginalCountry());
                    //普通VOD没有剧头
                    //resp.setParentCode();
                    //resp.setParentName();
                    //resp.setPhysicalContentID();
                    //resp.setPlayUrl();
                    //TODO URL规则待定
                    resp.setPoster(getHttpUrl(tcmsSeries.getPosterUrl()));
                    resp.setPrice(tcmsSeries.getPriceTaxIn());
                    resp.setProgramType(tcmsSeries.getType());
                    //TODO URL规则待定
                    resp.setRecommendImg(getHttpUrl(tcmsSeries.getRecommendUrl()));
                    //resp.setRecommendSeries();
                    resp.setReleaseYear(tcmsSeries.getReleaseYear());
                    resp.setSearchName(tcmsSeries.getSearchName());
                    resp.setStatus(tcmsSeries.getStatus());
                    //TODO URL规则待定
                    resp.setStillImg(getHttpUrl(tcmsSeries.getStillUrl()));
                    //resp.setStoreAddress();
                    //TODO URL规则待定
                    resp.setThumbnail(getHttpUrl(tcmsSeries.getThumbnailUrl()));
                    //resp.setVolumnCount();
                    resp.setWriters(tcmsSeries.getWriterDisplay());
                    resp.setHuaWeiCode(tmpTProgram.getHuaWeiCode());

                    if (!StringUtils.isEmpty(tmpTProgram.getHuaWeiPlayUrl())) {
                        resp.setHuaWeiPlayUrl(genSecurityMd5Url(tmpTProgram.getHuaWeiPlayUrl()));
                    }

                    if (!StringUtils.isEmpty(tmpTProgram.getZhongXingPlayUrl())) {
                        resp.setZhongXingPlayUrl(tmpTProgram.getZhongXingPlayUrl());
                    }
                    resp.setCmdId(tmpTProgram.getCmdID());
                    //内容是连续剧剧头
                } else if (tmpSeriesFlag == 1) {
                    resp.setCode(tcmsSeries.getCode());
                    resp.setName(tcmsSeries.getName());
                    //连续剧剧头不设置时长
                    //resp.setDuration();
                    //连续剧剧头不设置ProgramID
                    //resp.setProgramID();
                    //连续剧剧头不设置剧集号
                    //resp.setSetNumber();
                    //普通VOD，获取节目集数据
                    resp.setActor(tcmsSeries.getActorDisplay());
                    resp.setClarity(DefinitionFlag.toDesc(tcmsSeries.getSeriesFlag()));
                    resp.setCopyrightEndTime(tcmsSeries.getLicensingWindowEnd());
                    resp.setCopyrightStartTime(tcmsSeries.getLicensingWindowStart());
                    resp.setCpCode(tcmsSeries.getCpCode());
                    resp.setCreateTime(DateFormatUtil.format(tcmsSeries.getCreateTime(), "yyyyMMddHHmmss"));
                    resp.setDescription(tcmsSeries.getDescription());
                    resp.setDirector(tcmsSeries.getDirectorDisplay());
                    resp.setElementType(ElementType.SERIES.getType());
                    //resp.setHuaWeiPlayUrl();
                    resp.setLanguage(tcmsSeries.getLanguage());
                    //resp.setMappingID();
                    //resp.setMovieID();
                    resp.setOriginalCountry(tcmsSeries.getOriginalCountry());
                    //连续剧剧头不设置剧头
                    //resp.setParentCode();
                    //resp.setParentName();
                    //resp.setPhysicalContentID();
                    //resp.setPlayUrl();
                    //TODO URL规则待定
                    resp.setPoster(getHttpUrl(tcmsSeries.getPosterUrl()));
                    resp.setPrice(tcmsSeries.getPriceTaxIn());
                    resp.setProgramType(tcmsSeries.getType());
                    //TODO URL规则待定
                    resp.setRecommendImg(getHttpUrl(tcmsSeries.getRecommendUrl()));
                    //resp.setRecommendSeries();
                    resp.setReleaseYear(tcmsSeries.getReleaseYear());
                    resp.setSearchName(tcmsSeries.getSearchName());
                    resp.setStatus(tcmsSeries.getStatus());
                    //TODO URL规则待定
                    resp.setStillImg(getHttpUrl(tcmsSeries.getStillUrl()));
                    //resp.setStoreAddress();
                    //TODO URL规则待定
                    resp.setThumbnail(getHttpUrl(tcmsSeries.getThumbnailUrl()));
                    resp.setVolumnCount(tcmsSeries.getVolumnCount());
                    resp.setWriters(tcmsSeries.getWriterDisplay());
                    resp.setUpdateCount(tcmsSeries.getUpdateCount());
                    resp.setCmdId(tcmsSeries.getCmdId());
                }
            }
            log.info("获取内容详情成功");
        } catch (Exception e) {
            log.error("获取内容详情失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取内容详情失败，程序异常！");
        }
        return resp;

    }

    @Override
    public GetContentListResp getSeriesSubsetList(String seriesCode,
                                                  int firstIndex, int maxResult) {
        GetContentListResp resp = new GetContentListResp();
        if (StringUtils.isEmpty(seriesCode)) {
            log.error("剧头编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("剧头编码非法！");
            return resp;
        }
        try {
            TCmsSeries tcmsSeries = entityDao.get(TCmsSeries.class, "o.code=?1 and o.seriesFlag=1", new Object[]{seriesCode});
            if (tcmsSeries == null) {
                log.error("连续剧（" + seriesCode + "）不存在！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("连续剧不存在！");
                return resp;
            }

            /**
             * 获取产品编码与名称
             * 根栏目的parentCode及产品编码
             */
            List<TCmsMapping> tmpListTMapping = entityDao.getScrollData(TCmsMapping.class, "o.seriesCode=?1 and o.status=1", new Object[]{seriesCode});
            if (tmpListTMapping == null || tmpListTMapping.isEmpty()) {
                log.error("数据错误，栏目内容编排未上线或者内容未绑定栏目！");
                resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                resp.setResultDesc("数据错误，栏目内容编排未上线或者内容未绑定栏目！！");
                return resp;
            }
            String tmpProductCode;
            String tmpProductCodes = "";
            String tmpProductNames = "";
            TCmsProduct tmpTProduct;
            //for (TCmsMapping tmpTMapping : tmpListTMapping) {
            //    tmpProductCode = getProductCode(tmpTMapping.getCategoryCode());
            //    if (StringUtils.isEmpty(tmpProductCode)) {
            //        continue;
            //    }
            //    tmpTProduct = entityDao.get(TCmsProduct.class, "o.code=?1", new Object[]{tmpProductCode});
            //    if (tmpTProduct == null) {
            //        continue;
            //    }
            //    if (!("," + tmpProductCodes).contains("," + tmpProductCode + ",")) {
            //        tmpProductCodes = tmpProductCodes + tmpProductCode + ",";
            //        tmpProductNames = tmpProductNames + tmpTProduct.getName() + ",";
            //    }
            //}
            if (!StringUtils.isEmpty(tmpProductCodes)) {

                tmpProductCodes = StringUtils.removeEnd(tmpProductCodes, ",");
                tmpProductNames = StringUtils.removeEnd(tmpProductNames, ",");
            }

            String tmpSql = "select count(o.id) as count from TCmsProgram o where o.seriesCode=? and o.status=1";
            //不对节目状态做限制
            //String tmpSql = "select count(o.id) as count from TCmsProgram o where o.seriesCode=?";
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{seriesCode}, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }
            LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
            orderBy.put("serialNo", "asc");
            orderBy.put("createTime", "desc");
            List<TCmsProgram> tmpListTProgram = entityDao.getScrollData(TCmsProgram.class, firstIndex, maxResult, "o.seriesCode=?1 and o.status=1",
                    new Object[]{seriesCode}, orderBy);
            //不对节目状态做限制
            //List<TCmsProgram> tmpListTProgram = entityDao.getScrollData(TCmsProgram.class, firstIndex, maxResult, "o.seriesCode=?1",
            //		new Object[]{seriesCode}, orderBy);
            if (tmpListTProgram == null || tmpListTProgram.isEmpty()) {
                return resp;
            }
            List<GetContentListResp.ContentInfo> tmpListContentInfo = new ArrayList<GetContentListResp.ContentInfo>();
            GetContentListResp.ContentInfo tmpContentInfo;
            for (TCmsProgram tmpTProgram : tmpListTProgram) {

                tmpContentInfo = new GetContentListResp.ContentInfo();
                tmpContentInfo.setActor(tmpTProgram.getActorDisplay());
                tmpContentInfo.setClarity(DefinitionFlag.toDesc(tmpTProgram.getDefinitionFlag()));
                tmpContentInfo.setCopyrightEndTime(tmpTProgram.getLicensingWindowEnd());
                tmpContentInfo.setCopyrightStartTime(tmpTProgram.getLicensingWindowStart());
                tmpContentInfo.setCpCode(tmpTProgram.getCpCode());
                tmpContentInfo.setCreateTime(DateFormatUtil.format(tmpTProgram.getCreateTime(), "yyyyMMddHHmmss"));
                tmpContentInfo.setDescription(tmpTProgram.getDescription());
                tmpContentInfo.setDirector(tmpTProgram.getDirectorDisplay());
                tmpContentInfo.setCode(tmpTProgram.getCode());
                tmpContentInfo.setElementType(ElementType.CSERIES.getType());
                tmpContentInfo.setDuration(tmpTProgram.getDuration());
                tmpContentInfo.setProgramID(tmpTProgram.getCode());
                tmpContentInfo.setSetNumber(tmpTProgram.getSerialNo());
                tmpContentInfo.setName(tmpTProgram.getName());
                tmpContentInfo.setLanguage(tmpTProgram.getLanguage());
                //tmpContentInfo.setMappingID();
                //tmpContentInfo.setMovieID(movieID);
                tmpContentInfo.setOriginalCountry(tmpTProgram.getOriginalCountry());
                tmpContentInfo.setParentCode(tcmsSeries.getCode());
                tmpContentInfo.setParentName(tcmsSeries.getName());
                //tmpContentInfo.setPhysicalContentID();
                //tmpContentInfo.setPlayUrl();
                //TODO URL规则待定
                tmpContentInfo.setImgAddress(config.getImgAddress());
                tmpContentInfo.setPoster(getHttpUrl(tmpTProgram.getPosterUrl()));
                tmpContentInfo.setPrice(tmpTProgram.getPriceTaxIn());
                tmpContentInfo.setProgramType(tmpTProgram.getType());
                //TODO URL规则待定
                tmpContentInfo.setRecommendImg(getHttpUrl(tmpTProgram.getRecommendUrl()));
                //tmpContentInfo.setRecommendSeries();
                tmpContentInfo.setReleaseYear(tmpTProgram.getReleaseYear());
                tmpContentInfo.setSearchName(tmpTProgram.getSearchName());
                tmpContentInfo.setStatus(tmpTProgram.getStatus());
                //TODO URL规则待定
                tmpContentInfo.setStillImg(getHttpUrl(tmpTProgram.getStillUrl()));
                //具体影片文件才有存储地址
                //tmpContentInfo.setStoreAddress();
                //TODO URL规则待定
                tmpContentInfo.setThumbnail(getHttpUrl(tmpTProgram.getThumbnailUrl()));
                //tmpContentInfo.setVolumnCount();
                tmpContentInfo.setWriters(tmpTProgram.getWriterDisplay());
                tmpContentInfo.setHuaWeiCode(tmpTProgram.getHuaWeiCode());
                if (!StringUtils.isEmpty(tmpProductCodes)) {
                    tmpContentInfo.setProductCode(tmpProductCodes);
                    tmpContentInfo.setProductName(tmpProductNames);
                }

                if (!StringUtils.isEmpty(tmpTProgram.getHuaWeiPlayUrl())) {
                    tmpContentInfo.setHuaWeiPlayUrl(genSecurityMd5Url(tmpTProgram.getHuaWeiPlayUrl()));
                }

                if (!StringUtils.isEmpty(tmpTProgram.getZhongXingPlayUrl())) {
                    tmpContentInfo.setZhongXingPlayUrl(tmpTProgram.getZhongXingPlayUrl());
                }
                
                tmpContentInfo.setTags(tcmsSeries.getTags());
                tmpContentInfo.setCmdId(tmpTProgram.getCmdID());

                tmpListContentInfo.add(tmpContentInfo);
            }
            resp.setContentInfos(tmpListContentInfo);
            resp.setRecordSize(tmpIntCount);
            log.info("获取连续剧子集列表成功");
        } catch (Exception e) {
            log.error("获取连续剧子集列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取连续剧子集列表失败，程序异常！");
        }
        return resp;

    }

    @Override
    public GetContentListResp getRecommendList(String contentCode,
                                               String productCode, int firstIndex, int maxResult) {
        GetContentListResp resp = new GetContentListResp();
        log.info("开始获取内容推荐列表，contentCode["+contentCode+"],productCode["+productCode+"]");
        if (StringUtils.isEmpty(contentCode)) {
            log.error("内容编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("内容编码非法！");
            return resp;
        }

        try {
            //根据电影或子集编码推荐
            TCmsProgram tmpTProgram = entityDao.get(TCmsProgram.class, "o.code=?1", new Object[]{contentCode});
            TCmsSeries tmpTCmsSeries;
            boolean isProgram = false;
            if (tmpTProgram == null) {
                //根据剧头编码推荐
                tmpTCmsSeries = entityDao.get(TCmsSeries.class, "o.code=?1 and o.seriesFlag=1", new Object[]{contentCode});
                if (tmpTCmsSeries == null) {
                    log.error("节目集（" + contentCode + "）不存在！");
                    resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                    resp.setResultDesc("节目集不存在！");
                    return resp;
                }
            } else {
                tmpTCmsSeries = entityDao.get(TCmsSeries.class, "o.code=?1", new Object[]{tmpTProgram.getSeriesCode()});
                if (tmpTCmsSeries == null) {
                    log.error("内容（" + contentCode + "）节目集不存在！");
                    resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                    resp.setResultDesc("内容节目集不存在！");
                    return resp;
                }
                isProgram = true;
            }

            String tmpProgramType;
            String tmpDirector;
            String tmpActor;
            String name;
            if (isProgram) {
                tmpProgramType = tmpTProgram.getType();
                tmpDirector = tmpTProgram.getDirectorDisplay();
                tmpActor = tmpTProgram.getActorDisplay();
                name = tmpTProgram.getName();
            } else {
                tmpProgramType = tmpTCmsSeries.getType();
                tmpDirector = tmpTCmsSeries.getDirectorDisplay();
                tmpActor = tmpTCmsSeries.getActorDisplay();
                name = tmpTCmsSeries.getName();
            }
            String whereSql = "1=1";
            if (!StringUtils.isEmpty(tmpProgramType)) {
                whereSql = whereSql + " and (instr(o.type,'" + tmpProgramType + "')>0 or instr('" + tmpProgramType + "',o.type)>0)";
            } else {
                if (!StringUtils.isEmpty(tmpDirector)) {
                    whereSql = whereSql + " and (instr(o.directorDisplay,'" + tmpDirector + "')>0 or instr('" + tmpDirector + "',o.directorDisplay)>0)";
                } else {
                    if (!StringUtils.isEmpty(tmpActor)) {
                        whereSql = whereSql + " and (instr(o.actorDisplay,'" + tmpActor + "')>0 or instr('" + tmpActor + "',o.actorDisplay)>0)";
                    }
                }
            }
            if (whereSql.equals("1=1")) {
                whereSql = "instr(o.name,'" + name + "')>0 or instr('" + name + "',o.name)>0";
            }
            //产品为空时推荐所有产品下内容
            String tmpSql = "select count(distinct o.id) as count from TCmsSeries o, TCmsMapping p where " + whereSql
                    //+ " and o.code!=? and o.code=p.seriesCode and p.status=1";
                    //不对节目集状态做限制
                    + " and o.code!=? and o.code=p.seriesCode";
            String tmpCodeSql = "select distinct o.code as code from TCmsSeries o,TCmsMapping p where " + whereSql
                    //+ " and o.code!=? and o.code=p.seriesCode and p.status=1";
                    //不对节目集状态做限制
                    + " and o.code!=? and o.code=p.seriesCode";
            TCmsProduct tmpTProduct = null;
            if (!StringUtils.isEmpty(productCode)) {
                tmpTProduct = entityDao.get(TCmsProduct.class, "o.code=?1 and o.status=1", new Object[]{productCode});
                String tmpCategoryCodes = operationCache.getPCategory(productCode);
                if (!StringUtils.isEmpty(tmpCategoryCodes)) {
                    tmpSql = tmpSql + " and p.categoryCode in (" + tmpCategoryCodes + ")";
                    tmpCodeSql = tmpCodeSql + " and p.categoryCode in (" + tmpCategoryCodes + ")";
                }
            }
            tmpCodeSql = tmpCodeSql + " order by p.randomNumber desc,p.createTime desc";//p.sequence
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{tmpTCmsSeries.getCode()}, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }
            List<Map<String, Object>> tmpListMapCode = entityDao.getScrollDataByHql(tmpCodeSql, new Object[]{tmpTCmsSeries.getCode()}, firstIndex, maxResult);
            if (tmpListMapCode == null || tmpListMapCode.isEmpty()) {
                return resp;
            }
            List<GetContentListResp.ContentInfo> tmpListContentInfo = new ArrayList<GetContentListResp.ContentInfo>();
            GetContentListResp.ContentInfo tmpContentInfo;
            Integer tmpSeriesFlag;
            TCmsSeries tmpItem;
            String tmpSeriesCode;
            TCmsProgram tmpTCmsProgram;
            for (Map<String, Object> mapObject : tmpListMapCode) {
                tmpSeriesCode = (String) mapObject.get("code");
                if (StringUtils.isEmpty(tmpSeriesCode)) {
                    continue;
                }
                tmpItem = entityDao.get(TCmsSeries.class, "o.code=?1", new Object[]{tmpSeriesCode});
                if (tmpItem == null) {
                    continue;
                }
                tmpSeriesFlag = tmpItem.getSeriesFlag();
                if (tmpSeriesFlag == null) {
                    log.error("数据错误，内容元素类型为空！");
                    resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                    resp.setResultDesc("数据错误，内容元素类型为空！");
                    return resp;
                }
                tmpContentInfo = new GetContentListResp.ContentInfo();
                tmpContentInfo.setActor(tmpItem.getActorDisplay());
                tmpContentInfo.setClarity(DefinitionFlag.toDesc(tmpItem.getDefinitionFlag()));
                tmpContentInfo.setCopyrightEndTime(tmpItem.getLicensingWindowEnd());
                tmpContentInfo.setCopyrightStartTime(tmpItem.getLicensingWindowStart());
                tmpContentInfo.setCpCode(tmpItem.getCpCode());
                tmpContentInfo.setCreateTime(DateFormatUtil.format(tmpItem.getCreateTime(), "yyyyMMddHHmmss"));
                tmpContentInfo.setDescription(tmpItem.getDescription());
                tmpContentInfo.setDirector(tmpItem.getDirectorDisplay());
                tmpContentInfo.setTags(tmpItem.getTags());
                //0表示普通VOD，普通VOD有时长，节目programId
                if (tmpSeriesFlag == 0) {
                    tmpContentInfo.setElementType(ElementType.PROGRAM.getType());
                    tmpTCmsProgram = entityDao.get(TCmsProgram.class, "o.seriesCode=?1", new Object[]{tmpItem.getCode()});
                    if (tmpTCmsProgram != null) {
                        tmpContentInfo.setCode(tmpTCmsProgram.getCode());
                        tmpContentInfo.setElementType(ElementType.PROGRAM.getType());
                        tmpContentInfo.setDuration(tmpTCmsProgram.getDuration());
                        tmpContentInfo.setProgramID(tmpTCmsProgram.getCode());
                        tmpContentInfo.setSetNumber(tmpTCmsProgram.getSerialNo());
                        tmpContentInfo.setName(tmpTCmsProgram.getName());
                        tmpContentInfo.setHuaWeiCode(tmpTCmsProgram.getHuaWeiCode());

                        if (!StringUtils.isEmpty(tmpTCmsProgram.getHuaWeiPlayUrl())) {
                            tmpContentInfo.setHuaWeiPlayUrl(genSecurityMd5Url(tmpTCmsProgram.getHuaWeiPlayUrl()));
                        }

                        if (!StringUtils.isEmpty(tmpTCmsProgram.getZhongXingPlayUrl())) {
                            tmpContentInfo.setZhongXingPlayUrl(tmpTCmsProgram.getZhongXingPlayUrl());
                        }
                        tmpContentInfo.setCmdId(tmpTCmsProgram.getCmdID());
                    }

                    //1表示连续剧剧头，没有时长
                } else if (tmpSeriesFlag == 1) {
                    tmpContentInfo.setCode(tmpItem.getCode());
                    tmpContentInfo.setElementType(ElementType.SERIES.getType());
                    //剧头
                    tmpContentInfo.setParentCode(tmpItem.getCode());
                    tmpContentInfo.setParentName(tmpItem.getName());
                    tmpContentInfo.setName(tmpItem.getName());
                    tmpContentInfo.setVolumnCount(tmpItem.getVolumnCount());
                    tmpContentInfo.setUpdateCount(tmpItem.getUpdateCount());
                    tmpContentInfo.setCmdId(tmpItem.getCmdId());
                }
                tmpContentInfo.setLanguage(tmpItem.getLanguage());
                //tmpContentInfo.setMappingID();
                //tmpContentInfo.setMovieID(movieID);
                tmpContentInfo.setOriginalCountry(tmpItem.getOriginalCountry());
                //tmpContentInfo.setPhysicalContentID();
                //tmpContentInfo.setPlayUrl();
                //TODO URL规则待定
                tmpContentInfo.setPoster(getHttpUrl(tmpItem.getPosterUrl()));
                tmpContentInfo.setPrice(tmpItem.getPriceTaxIn());
                if (tmpTProduct != null) {
                    tmpContentInfo.setProductCode(productCode);
                    tmpContentInfo.setProductName(tmpTProduct.getName());
                }
                tmpContentInfo.setProgramType(tmpItem.getType());
                //TODO URL规则待定
                tmpContentInfo.setRecommendImg(getHttpUrl(tmpItem.getRecommendUrl()));
                //tmpContentInfo.setRecommendSeries();
                tmpContentInfo.setReleaseYear(tmpItem.getReleaseYear());
                tmpContentInfo.setSearchName(tmpItem.getSearchName());
                tmpContentInfo.setStatus(tmpItem.getStatus());
                //TODO URL规则待定
                tmpContentInfo.setStillImg(getHttpUrl(tmpItem.getStillUrl()));
                //具体影片文件才有存储地址
                //tmpContentInfo.setStoreAddress();
                //TODO URL规则待定
                tmpContentInfo.setThumbnail(getHttpUrl(tmpItem.getThumbnailUrl()));
                tmpContentInfo.setWriters(tmpItem.getWriterDisplay());

                tmpListContentInfo.add(tmpContentInfo);
            }
            resp.setContentInfos(tmpListContentInfo);
            resp.setRecordSize(tmpIntCount);
            log.info("获取内容推荐列表成功");
        } catch (Exception e) {
            log.error("获取内容推荐列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取内容推荐列表失败，程序异常！");
        }
        return resp;

    }


    @Override
    public GetScrollmsgListResp getScrollmsgList(long cpCode, int firstIndex,
                                                 int maxResult) {
        GetScrollmsgListResp resp = new GetScrollmsgListResp();
        try {
            String tmpWhereJpql = null;
            if (cpCode != -1) {
                tmpWhereJpql = "o.cpCode=" + cpCode + " and o.status=1";
            } else {
                tmpWhereJpql = "o.status=1";
            }
            String tmpCountHql = "select count(o.id) as count from com.zhehekeji.opsys.api.entity.TScrollmsg o where " + tmpWhereJpql;
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpCountHql, null, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }

            LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
            orderBy.put("createTime", "asc");
            List<TScrollmsg> tmpListTScrollmsg = entityDao.getScrollData(TScrollmsg.class, firstIndex, maxResult, tmpWhereJpql, null, orderBy);
            if (tmpListTScrollmsg == null || tmpListTScrollmsg.isEmpty()) return resp;
            List<GetScrollmsgListResp.ScrollmsgInfo> tmpListScrollmsgInfo = new ArrayList<GetScrollmsgListResp.ScrollmsgInfo>();
            GetScrollmsgListResp.ScrollmsgInfo tmpScrollmsgInfo;
            for (TScrollmsg tmpTScrollmsg : tmpListTScrollmsg) {
                tmpScrollmsgInfo = new GetScrollmsgListResp.ScrollmsgInfo();
                tmpScrollmsgInfo.setCode(tmpTScrollmsg.getCode());
                tmpScrollmsgInfo.setContent(tmpTScrollmsg.getContent());
                tmpScrollmsgInfo.setCpCode(tmpTScrollmsg.getCpCode());
                tmpListScrollmsgInfo.add(tmpScrollmsgInfo);
            }
            resp.setScrollmsgInfos(tmpListScrollmsgInfo);
            resp.setRecordSize(tmpIntCount);
            log.info("获取滚动信息列表成功！");
        } catch (Exception e) {
            log.error("获取滚动信息列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取滚动信息列表失败，程序异常！");
        }
        return resp;
    }

    @Override
    public GetScrollmsgDetailResp getScrollmsgDetail(String code) {
        GetScrollmsgDetailResp resp = new GetScrollmsgDetailResp();
        if (StringUtils.isEmpty(code)) {
            log.error("滚动信息编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("滚动信息编码非法！");
            return resp;
        }
        try {
            TScrollmsg tmpTScrollmsg = entityDao.get(TScrollmsg.class, "code=?1", new Object[]{code});
            if (tmpTScrollmsg == null) {
                log.error("滚动信息不存在，编码非法！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("滚动信息不存在，编码非法！");
                return resp;
            }
            Integer tmpStatus = tmpTScrollmsg.getStatus();
            if (tmpStatus != 1) {
                log.error("滚动信息未上线！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("滚动信息未上线");
                return resp;
            }
            resp.setCode(tmpTScrollmsg.getCode());
            resp.setContent(tmpTScrollmsg.getContent());
            resp.setCpCode(tmpTScrollmsg.getCpCode());
            log.info("获取滚动信息成功！");
        } catch (Exception e) {
            log.error("程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("程序异常");
        }
        return resp;
    }

    @Override
    public GetContentListResp getSearchList(String searchWord, String productCode, int firstIndex, int maxResult) {
        GetContentListResp resp = new GetContentListResp();
        String resultDesc = String.format("专区[%s]搜索[%s]", productCode, searchWord);
		/*if (StringUtils.isEmpty(StringUtils.trim(searchWord)) || StringUtils.isEmpty(StringUtils.trim(productCode))){
			resultDesc = resultDesc + "失败，参数非法！";
			log.error(resultDesc);
			resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}*/
        if (StringUtils.isEmpty(StringUtils.trim(searchWord))) {
            resultDesc = resultDesc + "失败，参数非法！";
            log.error(resultDesc);
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc(resultDesc);
            return resp;
        }
        try {
            String tmpSql = "select count(distinct o.id) as count from TCmsSeries o, TCmsMapping p "
                    + " where instr(o.searchName,?)>0 and p.status=1 and o.status=1 and o.code=p.seriesCode";
            String tmpCodeSql = "select distinct o.code as code from TCmsSeries o,TCmsMapping p "
                    + " where instr(o.searchName,?)>0 and p.status=1 and o.status=1 and o.code=p.seriesCode";
            TCmsProduct tmpTProduct = null;
            //if (!StringUtils.isEmpty(productCode)) {
            //    tmpTProduct = entityDao.get(TCmsProduct.class, "o.code=?1 and o.status=1", new Object[]{productCode});
            //    String tmpCategoryCodes = operationCache.getPCategory(productCode);
            //    if (!StringUtils.isEmpty(tmpCategoryCodes)) {
            //        tmpSql = tmpSql + " and p.categoryCode in (" + tmpCategoryCodes + ")";
            //        tmpCodeSql = tmpCodeSql + " and p.categoryCode in (" + tmpCategoryCodes + ")";
            //    }
            //}
            tmpCodeSql = tmpCodeSql + " order by p.randomNumber desc,p.createTime desc";//p.sequence
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{searchWord}, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }
            List<Map<String, Object>> tmpListMapCode = entityDao.getScrollDataByHql(tmpCodeSql, new Object[]{searchWord}, firstIndex, maxResult);
            if (tmpListMapCode == null || tmpListMapCode.isEmpty()) {
                return resp;
            }

            List<GetContentListResp.ContentInfo> tmpListContentInfo = new ArrayList<GetContentListResp.ContentInfo>();
            GetContentListResp.ContentInfo tmpContentInfo;
            Integer tmpSeriesFlag;
            TCmsSeries tmpItem;
            String tmpSeriesCode;
            TCmsProgram tmpTCmsProgram;
            for (Map<String, Object> mapObject : tmpListMapCode) {
                tmpSeriesCode = (String) mapObject.get("code");
                if (StringUtils.isEmpty(tmpSeriesCode)) {
                    continue;
                }
                tmpItem = entityDao.get(TCmsSeries.class, "o.code=?1 and o.status=1", new Object[]{tmpSeriesCode});
                if (tmpItem == null) {
                	log.info("节目集["+tmpSeriesCode+"]不存在");
                    continue;
                }
                tmpSeriesFlag = tmpItem.getSeriesFlag();
                if (tmpSeriesFlag == null) {
                    log.error("数据错误，内容元素类型为空！");
                    resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                    resp.setResultDesc("数据错误，内容元素类型为空！");
                    return resp;
                }
                tmpContentInfo = new GetContentListResp.ContentInfo();
                tmpContentInfo.setActor(tmpItem.getActorDisplay());
                tmpContentInfo.setClarity(DefinitionFlag.toDesc(tmpItem.getDefinitionFlag()));
                tmpContentInfo.setCopyrightEndTime(tmpItem.getLicensingWindowEnd());
                tmpContentInfo.setCopyrightStartTime(tmpItem.getLicensingWindowStart());
                tmpContentInfo.setCpCode(tmpItem.getCpCode());
                tmpContentInfo.setName(tmpItem.getName());
                tmpContentInfo.setCreateTime(DateFormatUtil.format(tmpItem.getCreateTime(), "yyyyMMddHHmmss"));
                tmpContentInfo.setDescription(tmpItem.getDescription());
                tmpContentInfo.setDirector(tmpItem.getDirectorDisplay());
                tmpContentInfo.setImgAddress(config.getImgAddress());
                tmpContentInfo.setTags(tmpItem.getTags());
                //0表示普通VOD，普通VOD有时长，节目programId
                if (tmpSeriesFlag == 0) {
                    tmpContentInfo.setElementType(ElementType.PROGRAM.getType());
                    tmpTCmsProgram = entityDao.get(TCmsProgram.class, "o.seriesCode=?1 and o.status=1", new Object[]{tmpItem.getCode()});
                    if (tmpTCmsProgram != null) {
                        tmpContentInfo.setCode(tmpTCmsProgram.getCode());
                        tmpContentInfo.setElementType(ElementType.PROGRAM.getType());
                        tmpContentInfo.setDuration(tmpTCmsProgram.getDuration());
                        tmpContentInfo.setProgramID(tmpTCmsProgram.getCode());
                        tmpContentInfo.setSetNumber(tmpTCmsProgram.getSerialNo());
                        tmpContentInfo.setName(tmpTCmsProgram.getName());
                        tmpContentInfo.setHuaWeiCode(tmpTCmsProgram.getHuaWeiCode());

                        if (!StringUtils.isEmpty(tmpTCmsProgram.getHuaWeiPlayUrl())) {
                            tmpContentInfo.setHuaWeiPlayUrl(genSecurityMd5Url(tmpTCmsProgram.getHuaWeiPlayUrl()));
                        }

                        if (!StringUtils.isEmpty(tmpTCmsProgram.getZhongXingPlayUrl())) {
                            tmpContentInfo.setZhongXingPlayUrl(tmpTCmsProgram.getZhongXingPlayUrl());
                        }
                        tmpContentInfo.setCmdId(tmpTCmsProgram.getCmdID());
                    }else{
                    	log.info("节目不存在");
                    }

                    //1表示连续剧剧头，没有时长
                } else if (tmpSeriesFlag == 1) {
                    tmpContentInfo.setCode(tmpItem.getCode());
                    tmpContentInfo.setElementType(ElementType.SERIES.getType());
                    //剧头
                    tmpContentInfo.setParentCode(tmpItem.getCode());
                    tmpContentInfo.setParentName(tmpItem.getName());
                    tmpContentInfo.setName(tmpItem.getName());
                    tmpContentInfo.setVolumnCount(tmpItem.getVolumnCount());
                    tmpContentInfo.setUpdateCount(tmpItem.getUpdateCount());
                    tmpContentInfo.setCmdId(tmpItem.getCmdId());
                }
                tmpContentInfo.setLanguage(tmpItem.getLanguage());
                //tmpContentInfo.setMappingID();
                //tmpContentInfo.setMovieID(movieID);
                tmpContentInfo.setOriginalCountry(tmpItem.getOriginalCountry());
                //tmpContentInfo.setPhysicalContentID();
                //tmpContentInfo.setPlayUrl();
                //TODO URL规则待定
                tmpContentInfo.setPoster(getHttpUrl(tmpItem.getPosterUrl()));
                tmpContentInfo.setPrice(tmpItem.getPriceTaxIn());
                if (tmpTProduct != null) {
                    tmpContentInfo.setProductCode(productCode);
                    tmpContentInfo.setProductName(tmpTProduct.getName());
                }
                tmpContentInfo.setProgramType(tmpItem.getType());
                //TODO URL规则待定
                tmpContentInfo.setRecommendImg(getHttpUrl(tmpItem.getRecommendUrl()));
                //tmpContentInfo.setRecommendSeries();
                tmpContentInfo.setReleaseYear(tmpItem.getReleaseYear());
                tmpContentInfo.setSearchName(tmpItem.getSearchName());
                tmpContentInfo.setStatus(tmpItem.getStatus());
                //TODO URL规则待定
                tmpContentInfo.setStillImg(getHttpUrl(tmpItem.getStillUrl()));
                //具体影片文件才有存储地址
                //tmpContentInfo.setStoreAddress();
                //TODO URL规则待定
                tmpContentInfo.setThumbnail(getHttpUrl(tmpItem.getThumbnailUrl()));
                tmpContentInfo.setWriters(tmpItem.getWriterDisplay());

                tmpListContentInfo.add(tmpContentInfo);
            }
            resp.setContentInfos(tmpListContentInfo);
            resp.setRecordSize(tmpIntCount);
            resultDesc = resultDesc + "成功！";
            log.info(resultDesc);
        } catch (Exception e) {
            resultDesc = resultDesc + "失败，程序异常！";
            log.error(resultDesc, e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc(resultDesc);
        }
        return resp;
    }

    @Override
    public GetChannelListResp getChannelList(Long cpCode, String zoneCode, int firstIndex, int maxResult) {
        GetChannelListResp resp = new GetChannelListResp();
        String resultDesc = String.format("获取频道,cpCode[%s]专区[%s]", cpCode, zoneCode);
        log.info(resultDesc);
        try {

            String tmpSql = "select count(o.id) as count from TCmsChannel o where 1=1 and o.status=1";
            String tmpJpqlWhere = "1=1 and o.status=1";
            if (cpCode != null && cpCode != -1) {
                tmpSql = tmpSql + " and o.cpCode=" + cpCode;
                tmpJpqlWhere = tmpJpqlWhere + " and o.cpCode=" + cpCode;
            }
            if (StringUtils.isNotBlank(zoneCode)) {
                tmpSql = tmpSql + " and o.zoneCode='" + StringEscapeUtils.escapeSql(zoneCode) +"'";
                tmpJpqlWhere = tmpJpqlWhere + " and o.zoneCode='" + StringEscapeUtils.escapeSql(zoneCode)+"'";
            }
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, null, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }

            LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
            orderBy.put("sequence", "desc");
            orderBy.put("createTime", "desc");
            List<TCmsChannel> tmpListTCategory = entityDao.getScrollData(TCmsChannel.class, firstIndex, maxResult, tmpJpqlWhere, null, orderBy);
            if (tmpListTCategory == null || tmpListTCategory.isEmpty()) {
                return resp;
            }

            List<GetChannelListResp.ChannelInfo> contentInfos = new ArrayList<>();
            for(TCmsChannel c:tmpListTCategory){
                GetChannelListResp.ChannelInfo info = new GetChannelListResp.ChannelInfo();
                info.setCode(c.getCode());
                info.setCpCode(c.getCpCode());
                info.setName(c.getChannelName());

                contentInfos.add(info);
            }

            resp.setChannleInfos(contentInfos);
            resp.setRecordSize(tmpIntCount);

        } catch (Exception e) {
            log.error("获取频道列表失败，程序异常", e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取频道列表失败，程序异常！");
        }

        return resp;
    }

    @Override
    public GetChannelSchedProgramListResp getChannelSchedProgramList(String channelCode, String day) {
        GetChannelSchedProgramListResp resp = new GetChannelSchedProgramListResp();
        String resultDesc = String.format("获取频道[%s]日期[%s]", channelCode, day);
        log.info(resultDesc);

        if (StringUtils.isEmpty(StringUtils.trim(channelCode))) {
            resultDesc = resultDesc + "失败，code为空！";
            log.error(resultDesc);
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc(resultDesc);
            return resp;
        }

        if (StringUtils.isEmpty(StringUtils.trim(day))) {
            resultDesc = resultDesc + "失败，日期为空！";
            log.error(resultDesc);
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc(resultDesc);
            return resp;
        }

        try{
            List<TCmsSchedule> ll = entityDao.getScrollData(TCmsSchedule.class, " o.channelCode=?1 and o.playDate=?2 and o.status=1", new Object[]{channelCode, day});
            if(CollectionUtils.isNotEmpty(ll)){
                List<GetChannelSchedProgramListResp.ChnlSchedProgramInfo> contentInfos = new ArrayList<>();
                TCmsSchedule tcmsSchedule = ll.get(0);
                LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
                orderBy.put("startPlayTime", "asc");
                List<TCmsSchedprogram> sps = entityDao.getScrollData(TCmsSchedprogram.class, -1, -1, "o.scheduleCode=?1 and o.status=1", new Object[]{tcmsSchedule.getCode()}, orderBy);
                int total = 0;
                if(CollectionUtils.isNotEmpty(sps)) {
                    int i = 1;
                    total = sps.size();
                    for (TCmsSchedprogram sp : sps) {
                        GetChannelSchedProgramListResp.ChnlSchedProgramInfo info = new GetChannelSchedProgramListResp.ChnlSchedProgramInfo();

                        info.setIndex(i);
                        info.setSeriesCode(sp.getSeriesCode());
                        info.setDuration(sp.getDuration());
                        info.setPlaytime(day+" "+sp.getStartPlayTime());
                        info.setEndtime(day+" "+sp.getEndPlayTime());
                        info.setSerieNo(sp.getSerieNo());
                        info.setAddress(genSecurityMd5Url(sp.getPlayUrl()));

                        contentInfos.add(info);
                        i++;
                    }
                }
                resp.setChnlSchedProgramInfos(contentInfos);
                resp.setRecordSize(total);
            }

        } catch (Exception e) {
            log.error("获取频道节目失败，程序异常", e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取频道节目失败，程序异常！");
        }

        return resp;
    }

    public String genSecurityMd5Url(String url) {
		//String url = "http://itv.100.ahct.lv1.vcache.cn/2300/2018/0514xlwj/2016/0516/0516.m3u8?stbCode=p55619586757&stbModel=E900";
		//http://itv.100.ahct.lv1.vcache.cn:9090/1434677091/9776c1968d77585e0d4dc3954b4f75c2/2300/2018/0514xlwj/2016/0516/0516.m3u8?stbCode=p55619586757&stbModel=E900
    	
    	String host = config.getVideoHost();//http://itv.100.ahct.lv1.vcache.cn
    	if(StringUtils.isBlank(host)) {
    		return url;
    	}
    	
    	if(StringUtils.isBlank(url)){
    		return url;
    	}
    	
    	if(!StringUtils.startsWith(url, host)){
    		return url;
    	}
    	
    	String skey = config.getSkey();
    	String urlExpiration = config.getUrlExpiration();
		String port = config.getVideoHostPort();//9090
		
		String path = StringUtils.removeStart(url, host);
		String last = StringUtils.substringBeforeLast(path, "/");//   /2300/2018/0514xlwj/2016/0516
		last = StringUtils.substringAfter(last, "/");//   2300/2018/0514xlwj/2016/0516
		
		//14346770912300/2018/0514xlwj/2016/0516itv100
		String md5 = Md5Utils.md5Encode(urlExpiration+last+skey);
		md5 = StringUtils.swapCase(md5);
		
		String finalUrl = host+":"+port+"/"+urlExpiration+"/"+md5+path;
		log.info("播放地址："+finalUrl+","+last);
		
		return finalUrl;
	}
}

